package com.jianyun.jyzs.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;


import com.jianyun.jyzs.ThisApp;
import com.jianyun.jyzs.constant.HttpConstant;

import org.apache.http.conn.util.InetAddressUtils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * Created by liml on 17/5/15.
 */

public class NetUtil {
    private static Uri PREFERRED_APN_URI = Uri
            .parse("content://telephony/carriers/preferapn");
    private static boolean isWIFI;
    private static boolean isMobile;

    /**
     * 判断网络类型
     *
     * @return
     */
    public static boolean checkNetType(Context context) {
        isWIFI = isWIFIConnectivity(context);
        isMobile = isMobileConnectivity(context);
        // 当前无可利用的通信渠道
        if (!isWIFI && !isMobile) {
            return false;
        }

        if (isMobile) {
            // WAP还是NET——如果是WAP读取到代理的信息，如果是NET代理信息为NULL
            // 如果为WAP 必须记录代理的ip 和 端口信息
            // readAPN(context);
        }

        return true;
    }

    /**
     * 获取当前正在链接的apn的信息
     */
    @SuppressWarnings("unused")
    private static void readAPN(Context context) {
        // ip和端口
        // 联系人
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(PREFERRED_APN_URI, null, null, null,null);
        if (cursor != null && cursor.moveToFirst()) {
            HttpConstant.PROXY_IP = cursor.getString(cursor.getColumnIndex("proxy"));
            HttpConstant.PROXY_PORT = cursor.getInt(cursor.getColumnIndex("port"));
        }

    }

    /**
     * 手机APN接入点
     *
     * @param context
     * @return
     */
    private static boolean isMobileConnectivity(Context context) {
        // ConnectivityManager---systemService---Context
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo = manager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (networkInfo != null) {
            return networkInfo.isConnected();
        }
        return false;
    }

    /**
     * 判断手机的链接渠道 WLAN（wi-fi）
     *
     * @param context
     * @return
     */
    public static boolean isWIFIConnectivity(Context context) {
        // ConnectivityManager---systemService---Context
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo != null) {
            return networkInfo.isConnected();

        }
        return false;
    }

    /**
     * 判断手机GPS是否开启
     */
    public static boolean getLocalGPSIsStart(Context context) {
        LocationManager location = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        return location
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    /**
     * 获取手机ip地址
     *
     * @return
     */
//    public static String getLocalIpAddress() {
//        try {
//            for (Enumeration<NetworkInterface> en = NetworkInterface
//                    .getNetworkInterfaces(); en.hasMoreElements();) {
//                NetworkInterface intf = en.nextElement();
//                for (Enumeration<InetAddress> enumIpAddr = intf
//                        .getInetAddresses(); enumIpAddr.hasMoreElements();) {
//                    InetAddress inetAddress = enumIpAddr.nextElement();
//                    if (!inetAddress.isLoopbackAddress()
//                            && InetAddressUtils.isIPv4Address(inetAddress
//                            .getHostAddress())) {
//                        return inetAddress.getHostAddress().toString();
//                    }
//                }
//            }
//        } catch (SocketException ex) {
//            throw new RuntimeException(ex.toString());
//        }
//        return null;
//    }

//    /**
//     * 判断网络类型
//     *
//     * @param context
//     * @return
//     */
    public static String getNetType(Context context) {
        boolean hasNetWork = checkNetType(context);
        if (hasNetWork) {
            if (isWIFI) {
                return "WIFI";
            } else if (isMobile) {
                if ((HttpConstant.PROXY_IP == "" || HttpConstant.PROXY_IP == null)
                        && HttpConstant.PROXY_PORT == 0) {
                    return "3G";
                } else {
                    return "2G";
                }
            }
        }
        return null;
    }
    public static boolean isNetworkConnected() {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) ThisApp.getInstance()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null) {
            return mNetworkInfo.isAvailable();
        }
        return false;
    }

    public static String getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf
                        .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()
                            && InetAddressUtils.isIPv4Address(inetAddress
                            .getHostAddress())) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
            throw new RuntimeException(ex.toString());
        }
        return "02:00:00:00:00:00";
    }
}
